import { AfterFetchContext, createFetch } from '@vueuse/core'
import { useUserStoreWidthOut } from '@renderer/store/modules/user'
import { useContextStoreWithout } from '@renderer/store/modules/context'
export const hotel = {
  武仙座: '8308',
  天坛座: '8307',
  天箭座: '8306',
  天鹰座: '8305',
  半人马座: '8304',
  巨爵座: '8303',
  六分仪座: '8302',
  南三角座: '8301'
}
export const transformNo = (no) => {
  const newNo = no?.replace(/[\d|\w]/g, '')
  if (Object.keys(hotel).includes(newNo)) {
    return hotel[newNo]
  }
  // 如果是以数字开头就是酒店
  if (/^\d/.test(no)) {
    return no.replace(/[^\d]/, '')
  }
  return no
}
const useHttp = createFetch({
  baseUrl: 'https://cdyydj.club/api',
  // baseUrl: 'http://127.0.0.1:7002/api',
  options: {
    async beforeFetch({ options }) {
      console.log(options)
      const user = useUserStoreWidthOut()
      const context: any = useContextStoreWithout()
      const token = user.GetToken
      if (token) {
        options.headers.Authorization = `Bearer ${token}`
      }
      if (context) {
        options.headers.YYDJ_STORE = context.store_id
        options.headers.YYDJ_NO = transformNo(context.no)
      }

      return { options }
    },
    async afterFetch(ctx: AfterFetchContext) {
      const user = useUserStoreWidthOut()
      const { code } = ctx.data
      if (code === 401 && !ctx.response.url.includes('/user/wxCommonLogin')) {
        user.loginout()
      }

      return ctx
    }
  }
})
export const useFetchHttp = (url: string) => {
  const user = useUserStoreWidthOut()
  const context1: any = useContextStoreWithout()
  const options: any = {}
  const params: any = {
    headers: {
      'Content-Type': 'application/json'
    }
  }
  const token = user.GetToken
  if (token) {
    params.headers.Authorization = `Bearer ${token}`
  }
  if (context1) {
    params.headers.YYDJ_STORE = context1.store_id
    params.headers.YYDJ_NO = transformNo(context1.no)
  }
  options.get = () => {
    params.method = 'GET'
    return options
  }

  options.post = (data) => {
    params.body = data
    return options
  }

  options.json = () => {
    return new Promise((resolve, reject) => {
      fetch('https://cdyydj.club/api' + url, params)
        .then((res) => res.json())
        .then((d) => {
          resolve({
            data: {
              value: d
            }
          })
        })
        .catch(reject)
    })
  }

  return options
}
export const useLocalHttp = createFetch({
  baseUrl: 'http://192.168.1.249:7005',
  options: {
    async beforeFetch({ options }) {
      const context = useContextStoreWithout()
      if (context) {
        options.headers.YYDJ_STORE = context.store_id
        options.headers.YYDJ_NO = transformNo(context.no)
      }

      return { options }
    },
    async afterFetch(ctx: AfterFetchContext) {
      // const user = useUserStoreWidthOut()
      // const { code } = ctx.data
      // if (code === 401 && !ctx.response.url.includes('/user/wxCommonLogin')) {
      //   user.loginout()
      // }

      return ctx
    }
  }
})

export default useHttp
